#!/usr/bin/env python
import socket
import os
import urllib2
import urlparse
import cookielib
import StringIO
import gzip
import logging
from configs import config
from logger import logger
from programinfo import info

def urlroot(s):
    o = urlparse.urlsplit(s)
    return o.scheme + '://' + o.netloc

def hostname():
    hostname = socket.gethostname()
    if not hostname: hostname = os.environ.get('HOSTNAME', '')
    return hostname

# Get working proxy settings based on current network situation
def getproxy():
    proxy = {}
    if config["PROXY"]:
        proxy = {'http': config["PROXY"]}

    return proxy

user_agent = "User-Agent: %s %s" % (info["name"], info["version"])
headers = { 
'User-Agent' : user_agent,
'Accept-Encoding' : "gzip",
}

class Session:
    def __init__(self, url=None, update_referer=True):
        self.req = None
        self._cj = cookielib.CookieJar()
        self.opener = urllib2.build_opener(urllib2.ProxyHandler(getproxy()), 
                                           urllib2.HTTPCookieProcessor(self._cj))
        self._referer = None
        self._url_handle = None
        if url is not None:
            self.open(url, update_referer)

    def open(self, url, update_referer=True):
        logger.info("Session: opening %s" % url)
        self.req = urllib2.Request(url, None, headers)
        if self._referer != None:
            self.req.add_header('Referer', self._referer)
            logger.debug("Session: set request Referer to %s" % self._referer)
        if update_referer:
            self._referer = url
        logger.debug("Session: request headers:\n%s" % self.req.headers)
        self._url_handle = self.opener.open(self.req)

    def response_info(self):
        return self._url_handle.info()

    def read(self):
        response = self.response_info()
        logger.debug("Session response info:\n%s" % response)
        if response.get('Content-Encoding') == 'gzip':
            logger.debug("Session: gzip encoding")
            buf = StringIO.StringIO(self._url_handle.read())
            gzip_file = gzip.GzipFile(fileobj=buf)
            doc = gzip_file.read()
        else:
            logger.debug("Session: normal encoding")
            doc = self._url_handle.read()
        logger.debug("Session: read %d bytes" % len(doc))
        return doc
        
    def close(self):
        return self._url_handle.close()

def readurl(url):
    session = Session(url)
    return session.read()

if __name__ == '__main__':
    logger.setLevel(logging.DEBUG)

    def test_Session():
        session = Session("http://www.baidu.com/")

        print "news baidu response info:\n%s\n" % session.response_info()

        session.open("http://www.google.com.hk/", False)
        print "google read bytes: %d\n" % len(session.read())

        session.open("http://news.163.com/")

        session.open("http://www.163.com/", False)
        print "163 read bytes: %d\n" % len(session.read())

    print "Proxy settings: %s\n" % getproxy()
    test_Session()

    print "readurl() read bytes: %d\n" % len(readurl("http://news.baidu.com"))
